package edu.spbu.eclipse.addressbook.andrpodk;

import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;


public class FindDialog extends SelectionAdapter implements KeyListener{
	private Shell dialogShell;
	private Text findField;
	private AddressBookEditor dataHandler;
	private Font font;
	public FindDialog(Shell parentShell,AddressBookEditor dataHandler,String lastFinding){
		this.dataHandler=dataHandler;
		dialogShell=new Shell(parentShell,SWT.DIALOG_TRIM|SWT.APPLICATION_MODAL);
		dialogShell.setText("Find");
		dialogShell.setBounds(350, 350, 255, 125);
		dialogShell.open();
		findField=new Text(dialogShell,SWT.SINGLE|SWT.BORDER);
		findField.setBounds(25, 22, 200, 23);
		findField.setFont(font=new Font(parentShell.getDisplay(),"Common",11,SWT.NORMAL));
		findField.setFocus();
		findField.setText(lastFinding);
		findField.selectAll();
		findField.addKeyListener(this);
		Button go=new Button(dialogShell,SWT.PUSH);
		go.setText("Go");
		go.setBounds(185, 57, 40, 25);
		go.addKeyListener(this);
		go.addSelectionListener(this);				
	}
	public void widgetSelected(SelectionEvent e){
		String string=findField.getText();
		if(!string.equals("")){
			dialogShell.dispose();
			font.dispose();
			dataHandler.handleFindData(string);			
		}else{
			findField.setFocus();
		}
	}
	public void keyReleased(KeyEvent e) {}
	public void keyPressed(KeyEvent e) {		
		if(e.character==SWT.CR){
			widgetSelected(null);			
		}
	}		
}
